Documentation for Users  2.1.2
Perception Toolbox for Virtual Reality (PTVR) Manual
laser_pointing.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 """
3 ...\PTVR_Researchers\Python_Scripts\Demos\Pointing\laser_pointing.py
4 
5 
6 Goal : illustrate the use of the Pointing Laser.
7 
8 
9 What you see: if you point at the red sphere (sphere_1) with the laser beam,
10  it will turn blue. This is an INTERACTION between an EVENT and a
11  CALLBACK.
12 
13 Note on the pointing cone (surrounding the laser beam):
14  It can be displayed or not thanks to the 'display_pointing_cone'
15  parameter (True or False). The default in this demo is True.
16 
17 
18 Important note concerning scripts that use handcontrollers:
19  It is necessary to specify your VR system's manufacturer if:
20  a/ your code contains a PointedAt Event triggered by a handcontroller
21  or
22  b/ your code contains a PointingLaser () object.
23  This is specified in the The3DWorld() in the following way:
24  headset_manufacturer = "Vive" (default) refers to the HTC Vive pro series headsets.
25  headset_manufacturer = "Oculus" refers to the Quest 1, 2 or 3 headsets (in keeping
26  with OpenXR terminology).
27  For example:
28 my_world = The3DWorld (
29  headset_manufacturer = "Vive" # "Vive" by default
30  )
31 
32 Things to check in the PointingLaser object when using different
33  headset manufacturers:
34  ex. With the Quest, you should do
35  laser_beam_x_y_rotation = np.array([60, 0]) # 60 ° about X axis
36  in the PointingLaser () object.
37  Otherwise the laserbeam will be directed upwards in an awkward and non
38  ergonomic direction.
39 See details on the laser beam axis in the laser_beam_axis.py demo script.
40 
41 """
42 
43 
44 from PTVR.Visual import The3DWorld
45 from PTVR.Stimuli.Scenes import VisualScene
46 from PTVR.Stimuli.Objects import Sphere
47 import PTVR.Stimuli.Color as color
48 import numpy as np
49 from PTVR.Pointing.PointingCursor import PointingLaser, LaserContingency
50 from PTVR.SystemUtils import LaunchThe3DWorld
51 import PTVR.Data.Callback
52 import PTVR.Data.Event
53 # =============================================================================
54 # PARAMETERS #
55 # =============================================================================
56 
57 # Laser parameters
58 my_laser_color = color.RGBColor(r=0.7, g=0.5)
59 hand = LaserContingency.RIGHT_HAND
60 my_activation_cone_radius_deg = 2
61 
62 
63 sphere_size = np.array([1, 1, 1])
64 
65 sphere_1_color = color.RGBColor(1, 0, 0)
66 sphere_1_pos = np.array([-2, 0, 3])
67 sphere_2_color = color.RGBColor(0, 1, 0)
68 sphere_2_pos = np.array([0, 0, 5])
69 sphere_3_color = color.RGBColor(0, 0, 1)
70 sphere_3_pos = np.array([2, 0, 7])
71 new_color = color.RGBColor(b=1, g=1)
72 # =============================================================================
73 # END PARAMETERS #
74 # =============================================================================
75 
76 my_world = The3DWorld(
77  headset_manufacturer = "Vive"
78  # "Vive" (default) or "Oculus" or "ViveFocusVision"
79 )
80 # It is necessary to specify your VR system's manufacturer (here "Vive")
81 # in the The3DWorld() object if:
82 # a/ your code contains a PointedAt Event triggered by a handcontroller
83 # or
84 # b/ your code contains a PointingLaser () Object.
85 
86 
87 def main():
88 
89  if (hand == LaserContingency.RIGHT_HAND):
90  is_right_controller_visibility = True
91  is_left_controller_visibility = False
92  my_activation_cone_origin_id = my_world.handControllerRight.id
93  if (hand == LaserContingency.LEFT_HAND):
94  is_left_controller_visibility = True
95  is_right_controller_visibility = False
96  my_activation_cone_origin_id = my_world.handControllerLeft.id
97 
98  my_scene = VisualScene(is_left_hand_controller_visible=is_left_controller_visibility,
99  is_right_hand_controller_visible=is_right_controller_visibility)
100 
101  # Create a pointer
102  my_laser_beam = PointingLaser(laser_hand=hand,
103  laser_color=my_laser_color,
104  laser_width=0.01)
105  my_scene.place_pointing_laser(my_laser_beam)
106 
107  # Creation of Spheres
108  sphere_1 = Sphere(size_in_meters=sphere_size, color=sphere_1_color,
109  position_in_current_CS=sphere_1_pos)
110  sphere_2 = Sphere(size_in_meters=sphere_size, color=sphere_2_color,
111  position_in_current_CS=sphere_2_pos)
112  sphere_3 = Sphere(size_in_meters=sphere_size, color=sphere_3_color,
113  position_in_current_CS=sphere_3_pos)
114 
115  my_scene.place(sphere_1, my_world)
116  my_scene.place(sphere_2, my_world)
117  my_scene.place(sphere_3, my_world)
118 
119  # Interactions
120  # Events
121  sphere_1_is_pointed_at = PTVR.Data.Event.PointedAt(
122  target_id = sphere_1.id,
123  activation_cone_origin_id = my_activation_cone_origin_id,
124  activation_cone_radius_deg = my_activation_cone_radius_deg,
125  display_pointing_cone = True # you can display, or not, the
126  # pointing cone here.
127  )
128  sphere_1_is_not_pointed_at = PTVR.Data.Event.PointedAt(
129  target_id = sphere_1.id,
130  activation_cone_origin_id = my_activation_cone_origin_id,
131  activation_cone_radius_deg = my_activation_cone_radius_deg,
132  display_pointing_cone = True,
133  mode = "release"
134  )
135  # Callbacks
136  change_sphere_1_color = PTVR.Data.Callback.ChangeObjectColor(
137  object_id=sphere_1.id,
138  new_color=new_color)
139  change_sphere_1_color_back = PTVR.Data.Callback.ChangeObjectColor(
140  object_id=sphere_1.id,
141  effect="deactivate")
142 
143  my_scene.AddInteraction(events=[sphere_1_is_pointed_at],
144  callbacks=[change_sphere_1_color])
145  my_scene.AddInteraction(events=[sphere_1_is_not_pointed_at],
146  callbacks=[change_sphere_1_color_back])
147 
148  my_world.add_scene(my_scene)
149  my_world.write()
150 
151 
152 if __name__ == "__main__":
153  main()
154  LaunchThe3DWorld() # Launch the Experiment with PTVR.
def LaunchThe3DWorld(jsonFileCategory="Externals")
Definition: SystemUtils.py:182